'''
'   This sample demonstrates call logging using tapi library
'
'
'
Imports OpenNETCF.Telephony

Public Class Form1
    Inherits System.Windows.Forms.Form
    Friend WithEvents lvCallLog As System.Windows.Forms.ListView
    Friend WithEvents hdrDirection As System.Windows.Forms.ColumnHeader
    Friend WithEvents hdrNumber As System.Windows.Forms.ColumnHeader
    Friend WithEvents hdrDuration As System.Windows.Forms.ColumnHeader
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu

    Friend WithEvents tapiObj As New Telephony
    Friend WithEvents lineObj As Line
    Friend WithEvents callObj As [Call]

    Private CallStart As DateTime
    Private CurrentLogItem As Integer = -1

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        MyBase.Dispose(disposing)
    End Sub

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Private Sub InitializeComponent()
        Me.MainMenu1 = New System.Windows.Forms.MainMenu
        Me.lvCallLog = New System.Windows.Forms.ListView
        Me.hdrDirection = New System.Windows.Forms.ColumnHeader
        Me.hdrNumber = New System.Windows.Forms.ColumnHeader
        Me.hdrDuration = New System.Windows.Forms.ColumnHeader
        '
        'lvCallLog
        '
        Me.lvCallLog.Columns.Add(Me.hdrDirection)
        Me.lvCallLog.Columns.Add(Me.hdrNumber)
        Me.lvCallLog.Columns.Add(Me.hdrDuration)
        Me.lvCallLog.Location = New System.Drawing.Point(0, 8)
        Me.lvCallLog.Size = New System.Drawing.Size(240, 248)
        Me.lvCallLog.View = System.Windows.Forms.View.Details
        '
        'hdrDirection
        '
        Me.hdrDirection.Text = "Dir"
        Me.hdrDirection.Width = 30
        '
        'hdrNumber
        '
        Me.hdrNumber.Text = "Number"
        Me.hdrNumber.Width = 140
        '
        'hdrDuration
        '
        Me.hdrDuration.Text = "Duration"
        Me.hdrDuration.Width = 68
        '
        'Form1
        '
        Me.Controls.Add(Me.lvCallLog)
        Me.Menu = Me.MainMenu1
        Me.MinimizeBox = False
        Me.Text = "Call logger"

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Initialize TAPI
        tapiObj.Initialize()
        lineObj = tapiObj.GetCellularLine(MediaMode.InteractiveVoice, CallPrivilege.Monitor)

    End Sub

    ' New call handling
    Private Sub lineObj_NewCall(ByVal [call] As OpenNETCF.Telephony.Call) Handles lineObj.NewCall
        ' Store call object for future use
        callObj = [call]

        ' Create new log entry
        Dim item As New ListViewItem(New String() {"", "", ""})
        item.Text = IIf((callObj.State = CallState.Offering), "<--", "-->")
        If Not callObj.CallerID Is Nothing AndAlso callObj.CallerID <> "" Then
            item.SubItems(1).Text = callObj.CallerID
        End If
        lvCallLog.Items.Add(item)
        CurrentLogItem = item.Index

        ' Start call timer
        CallStart = DateTime.Now
    End Sub

    ' Handle call disposal on idle state
    Private Sub callObj_CallState(ByVal [call] As OpenNETCF.Telephony.Call, ByVal state As OpenNETCF.Telephony.CallState) Handles callObj.CallState
        Select Case state
            Case CallState.Idle
                If [call] Is callObj And CurrentLogItem > -1 Then
                    Dim item As ListViewItem = lvCallLog.Items(CurrentLogItem)
                    Dim length As TimeSpan = DateTime.Now.Subtract(CallStart)
                    item.SubItems(2).Text = length.ToString()
                    CurrentLogItem = -1
                    callObj.Dispose()
                    callObj = Nothing
                End If

        End Select
    End Sub

    ' Handle call info update
    Private Sub callObj_CallInfo(ByVal [call] As OpenNETCF.Telephony.Call, ByVal infoState As OpenNETCF.Telephony.CallInfoState, ByVal info As OpenNETCF.Telephony.CallInformation) Handles callObj.CallInfo
        ' If our call and has caller ID and there is a current entry
        If [call] Is callObj And CurrentLogItem > -1 Then
            Dim item As ListViewItem = lvCallLog.Items(CurrentLogItem)
            item.Text = CType(IIf(callObj.Info.Origin = CallOrigin.Outbound, "-->", "<--"), String)
            item.SubItems(1).Text = CType(IIf(callObj.Info.Origin = CallOrigin.Outbound, callObj.Info.CalledID, callObj.Info.CallerID), String)
        End If
    End Sub

    ' Cleanup
    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        tapiObj.Shutdown()
    End Sub

End Class
